<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */
class WWW_model_organizations extends WWW_Factory {
	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		/* require('engine/class.www-database.php');
		$this->conn=new WWW_Database();		 */		
		require_once('engine/include_dao.php');
		return true;
	}
	public function create_organization($data){
		$organization = new Organization();
		$organization->organization_name = $data['organization_name'];
		$organization->organization_url = $data['organization_url'];
		$organization->status = $data['status'];
		$organization->created_date = date("Y-m-d H:i:s");
		$organization->updated_date = date("Y-m-d H:i:s");
		DAOFactory::getOrganizationsDAO()->insert($organization);
		$result = $organization->organization_id;
		if($result!=""){
				return array('message'=>'organization successfully created');			
			}else{
				return array('message'=>'organization Creation Failed');
			}	
	}
	public function edit_organization($data){
		$organization = new Organization();
		$organization->organization_name = $data['organization_name'];
		$organization->organization_url = $data['organization_url'];
		$organization->organization_id = $data['organization_id'];
		$organization->status = $data['status'];
		$organization->updated_date = date("Y-m-d H:i:s");
		$organization->created_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getOrganizationsDAO()->update($organization);
			if($result == 1){
				return array('message'=>'Updated Successfully');			
			}else{
				return array('message'=>'Updates Failed');
			}	
	}
	public function get_organization($data)
	{	
		$organization = new Organization();
		$organization->organization_id = $data['organization_id'];
		$result = DAOFactory::getOrganizationsDAO()->load($organization);
		if($result){
			return array('message'=>'organization get Successfully', 'result'=>$result);
		}else{
			return array('message'=>'organization get Failed');
		}	
	}
	public function delete_organization($data){
		$organization = new Organization();
		$organization->organization_id = $data['organization_id'];
		$organization->status = $data['status'];
		$result = DAOFactory::getOrganizationsDAO()->update_status($organization);
		 
		if($result == 1)
			return array('message'=>"organization Deleted Successfully");		
		else{
			return array('message'=>'organization Deletion Fails');
		}
	}
	public function all_organizations($data)
	{
		$current_page=0;
		$results_per_page=10;
		$order_by="created_date";
		$sort_order="desc";
		if(isset($data['order_by']) && $data['order_by']!='')
		{
			$order_by=$data['order_by'];
		}
		if(isset($data['sort_order']) && $data['sort_order']!='')
		{
			$sort_order=$data['sort_order'];
		}
		if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
		{
		$current_page=$data['current_page'];
		}
		if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
		$results_per_page=$data['results_per_page'];
		}
		$offset=$current_page;
		if (isset($data['search_by']) && $data['search_by']!='')
		{	
			if($data['search_by']=='organization_name')
			{
				$search_by = "organization_name";
				
			}else if($data['search_by'] == 'organization_url')
			{
				$search_by = "organization_url";
			}
			$organization = new Organization();
			$organization->current_page = $offset;
			$organization->order_by = $order_by;
			$organization->sort_order = $sort_order;
			$organization->results_per_page = $results_per_page;
			$organization->search_by = $search_by;
			$organization->search_by_value = $data['search_by_value'];
			$result = DAOFactory::getOrganizationsDAO()->queryAllsearchby($organization);
			$totalrows = DAOFactory::getOrganizationsDAO()->searchby_count($organization);
		}
		else
		{
			$organization = new Organization();
			$organization->current_page = $offset;
			$organization->order_by = $order_by;
			$organization->sort_order = $sort_order;
			$organization->results_per_page = $results_per_page;
			$result = DAOFactory::getOrganizationsDAO()->queryAll($organization);
			$totalrows = DAOFactory::getOrganizationsDAO()->queryAllCount($organization);
		}
			if($result){
				
				return array('message'=>"All organizations","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
				}
				else{
				return array('message'=>'No organizations');
				}
	}	
	
}
?>